<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.clientUserName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="课程类型" prop="courseItemType">
        <el-select
          v-model="queryParams.courseItemType"
          placeholder="请选择课程类型"
          clearable
          class="!w-240px"
        >
          <el-option label="固定课程" :value="1" />
          <el-option label="定制课程" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="开通状态" prop="courseStatus">
        <el-select
          v-model="queryParams.courseStatus"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option label="已开通" :value="1" />
          <el-option label="未开通" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['course:course-open:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="用户昵称/头像" align="center">
        <template #default="scope">
          <div class="w-full flex justify-center items-center">
            <img class="w-10 h-10 rounded-full" :src="scope.row.avatar" />
            <div class="ml-3 text-base">{{ scope.row.nickname }}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="课程" align="center">
        <template #default="scope">
          <div class="w-full flex justify-center items-center">
            <img class="w-10 h-10 rounded-full" :src="scope.row.courseCoverUrl" />
            <div class="ml-3 text-base">{{ scope.row.courseName }}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="课程有效天数"
        align="center"
        prop="validDays"
        :formatter="validDaysFormatter"
      />
      <el-table-column
        label="课程开通时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
      />
      <el-table-column
        label="课程到期时间"
        align="center"
        prop="createTime"
        :formatter="validFormatter"
      />
      <!-- <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['course:course-open:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['course:course-open:update']"
          >
            删除
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <CourseOpenForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts" name="Service">
import { dateFormatter } from '@/utils/formatTime'
import * as CourseApi from '@/api/course/index'
import CourseOpenForm from './CourseOpenForm.vue'

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref<CourseApi.MemberCourseMappingVO[]>([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  id: null,
  nickName: null,
  avatar: null,
  courseName: null,
  courseItemType: null,
  courseStatus: null,
  validDaysDays: null,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    // const data = await CourseOpenApi.getCourseOpenPage(queryParams)
    const data = await CourseApi.getMemberCourseMappingPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await CourseOpenApi.deleteCourse(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

// 有效天数格式化
const validDaysFormatter = (row, _column, value, _index) => {
  if (row.validDays === 0) {
    return '永久有效'
  }
  return value + '天'
}

// 到期时间格式化
const validFormatter = (row, _column, value, _index) => {
  if (row.validDays === 0) {
    return '永久有效'
  }
  let remainder = value + row.validDays * 24 * 60 * 60 * 1000
  let date = new Date(remainder).toLocaleDateString().replace(/\//g, '-')
  let time = new Date(remainder).toLocaleTimeString()
  return date + ' ' + time
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
